﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using NHibernate;
using NHibernate.Cfg;

namespace HelloNHibernate
{
    public class DAL
    {


        public DAL()
        {
            Configuration c = new Configuration();
            c.AddXmlFile("Model\\Employee.hbm.xml");
      //    c.AddAssembly(Assembly.GetCallingAssembly());
            sessionFactory = c.BuildSessionFactory();
        }

        public static ISessionFactory sessionFactory = null;

        private static ISession OpenSession()
        {
            
            return sessionFactory.OpenSession();
        }    
     

  

       public void CreateEmployeeAndSaveToDatabase()
        {
           
            Employee tobin = new Employee();
            tobin.Name = "Tobin Harrisss";
            using (ISession session = OpenSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    session.Save(tobin);
                    transaction.Commit();
                }
                //txtBox.Text += ("Saved Tobin to the database");

            }
        }

       public void CreateResearchAndSaveToDatabase()
       {
           Research  testResearch = new Research();
           testResearch.Name = "Test Harrisss";
           testResearch.Summary = "SVasta nesta";
           testResearch.AccessTime = DateTime.Now;
           testResearch.CreateTime = DateTime.Now;

           using (ISession session = OpenSession())
           {
               using (ITransaction transaction = session.BeginTransaction())
               {
                   session.Save(testResearch);
                   transaction.Commit();
               }
               //txtBox.Text += ("Saved Tobin to the database");

           }
       }


       public IList<Employee> LoadEmployeesFromDatabase()
        {
            using (ISession nhibernateSession = OpenSession())
            {
                IQuery query = nhibernateSession.CreateQuery(
                "from Employee as emp order by emp.name asc");
                IList<Employee> foundEmployees = query.List<Employee>();

                return foundEmployees;              
            }
        }

       public IList<Research> LoadResearchesFromDatabase()
       {
           using (ISession nhibernateSession = OpenSession())
           {
               IQuery query = nhibernateSession.CreateQuery(
               "from Research as res order by res.name asc");
               IList<Research> foundResearchs = query.List<Research>();

               return foundResearchs;
           }
       }

       public void UpdateTobinAndAssignPierreAsManager()
        {
            using (ISession session = OpenSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    IQuery q = session.CreateQuery("from Employee where name = 'Tobin Harris'");
                    Employee tobin = q.List<Employee>()[0];
                    tobin.Name = "Tobin David Harris";
                    Employee pierre = new Employee();
                    pierre.Name = "Pierre Henri Kuate";
                    tobin.manager = pierre;
                    session.Flush();
                    transaction.Commit();
                    //txtBox.Text += ("Updated Tobin and added Pierre");

                }
            }
        }
    }
}
